//引入样式
require("../../assets/css/reset.css")
require('../../assets/css/normalize.css')
require('../../assets/fonts/iconfont.css')
require('../../assets/css/tabbar.less')
require('./index.less');
//引入swpier
require('../../libs/swiper/swiper-bundle.css')
const Swiper = require('../../libs/swiper/swiper-bundle.js');

//引入模块
const dom = require('../../utils/dom.js');
const axios = require('axios');

document.addEventListener("DOMContentLoaded", function () {
    // 渲染tabbar
    dom.tabbar("index");
    //轮播图
    var mySwiper = new Swiper('.swiper', {
        loop: true, // 循环模式选项
        autoplay: {
            delay: 3000,
            stopOnLastSlide: false,
            // 交互后继续执行需要设置为false
            disableOnInteraction: false,
        },
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        }
    })

    // 获取token
    let token = localStorage.getItem('token');
    //获取token后才发送请求获取用户个人信息
    //发送请求获取用户个人信息
    axios.defaults.baseURL = 'http://fitness.h5.itsource.cn:3701'; //设置AJAX基础路径
    axios.defaults.headers.common['Authorization'] = 'Bearer ' + token;
    axios.get('/api/user/info').then(res => {
        //渲染排名
        dom.get('#rankNum').textContent = res.data.data.ranking;
        //渲染徽章
        dom.get('#badgeNum').textContent = res.data.data.badges;
    });
    //获取用户打卡信息
    function renderClock() {
        axios.get('/api/user/clockInInfo').then(res => {
            dom.get('#clockNum').textContent = res.data.data.clockCount;
            if (res.data.data.isClockIn) {
                dom.get('#checkIn-btn').disabled = true;
                dom.get('#checkIn-btn').textContent = '已打卡';
            }
        })
    }
    renderClock();
    // 绑定打卡事件
    dom.get('#checkIn-btn').addEventListener("click", function () {
        //发请求打卡
        axios.post('/api/user/clockIn').then(res => {
            if (res.data.errno === 0) {
                alert('今日打卡成功');
                renderClock();
            }
        });
    })
})